package com.yunxi.core.util;

import com.yunxi.core.web.DateFormat;

import java.text.*;
import java.util.*;
import java.util.regex.Pattern;

public class WgrDate
{
	private static Calendar	calendar	= Calendar.getInstance();

	private static int		year;

	private static int		month;

	private static int		day;

	private static int		hour;

	private static int		minute;

	private static int		second;

	private static long		time;
	
	private static int      quarter;
	
	private static final SimpleDateFormat formatter = new SimpleDateFormat();
	
	public static int getDay()
	{
		day = calendar.get(5);
		return day;
	}
	
	public static String getDayStr(){
	    day = 1 + calendar.get(5);
        if(day<10)
            return "0"+day;
        return day+"";
	}

	public static int getHour()
	{
		hour = calendar.get(11);
		return hour;
	}

	public static int getMinute()
	{
		minute = calendar.get(12);
		return minute;
	}

	public static int getMonth()
	{
		month = 1 + calendar.get(2);
		return month;
	}
	
	public static String getMonthStr()
    {
        month = 1 + calendar.get(2);
        if(month<10)
            return "0"+month;
        return month+"";
    }

	public static int getSecond()
	{
		second = calendar.get(13);
		return second;
	}

	public static long getTime()
	{
		return time;
	}

	public static int getYear()
	{
		calendar = Calendar.getInstance();
		year = calendar.get(1);
		return year;
	}

	public static String getCurrentTime()
	{
		SimpleDateFormat bartDateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");

		Date date = new Date();
		String a = bartDateFormat.format(date);

		return a;
	}
	
	public static String getCurYearDay(){
	    return getYear()+"-01-01";
	}
	
	public static String getCurYearLastDay(){
	    return getYear()+"-12-31";
	}
	
	public static String getCurMonthFirstDay(){
	    if(getMonth()<10){
	        return getYear()+"-0"+getMonth()+"-01";
	    }
	    return getYear()+"-"+getMonth()+"-01";
	}
	
	public static String getCurMonthLastDay(){
	    if(getMonth()<10){
            return getYear()+"-0"+getMonth()+"-" + getDayNumInMonth(getCurrentDateStr());
        }
        return getYear()+"-"+getMonth()+"-" + getDayNumInMonth(getCurrentDateStr());
	}

	public static String getCurrentTime(int type)
	{
		String format = "yyyy-MM-dd HH:mm:ss";
		if (type == 1)
		{
			format = "yyyy";
		}
		else if (type == 2)
		{
			format = "yyyy-MM";
		}
		else if (type == 3)
		{
			format = "yyyy-MM-dd";
		}
		else if (type == 4)
		{
			format = "yyyy-MM-dd HH";
		}
		else if (type == 5)
		{
			format = "yyyy-MM-dd HH:mm";
		}

		SimpleDateFormat bartDateFormat = new SimpleDateFormat(format);

		Date date = new Date();
		String a = bartDateFormat.format(date);

		return a;
	}

	public static String getCurrentTime(String format)
	{
		SimpleDateFormat bartDateFormat = new SimpleDateFormat(format);

		Date date = new Date();
		String a = bartDateFormat.format(date);

		return a;
	}

	public static String getCurrentDateStr()
	{
		SimpleDateFormat bartDateFormat = new SimpleDateFormat(
		"yyyy-MM-dd");

		Date date = new Date();
		String a = bartDateFormat.format(date);

		return a;
	}

	public static String getCurrentDateStr2()
	{
		SimpleDateFormat bartDateFormat = new SimpleDateFormat(
				"yyyyMMdd");

		Date date = new Date();
		String a = bartDateFormat.format(date);

		return a;
	}

	public static String getDisplayDate()
	{
		return MessageFormat.format("{0,time,yyyy-MM-dd}",
		new Object[]
		{ getCurrentTime() });
	}

	public static String getDisplayYMDate()
	{
		return MessageFormat.format("{0,time,yyyy-MM}",
		new Object[]
		{ getCurrentTime() });
	}

	public static String getDisplayYDate()
	{
		return MessageFormat.format("{0,time,yyyy}",
		new Object[]
		{ getCurrentTime() });
	}

	public static String getDisplayMDate()
	{
		return MessageFormat.format("{0,time,MM}",
		new Object[]
		{ getCurrentTime() });
	}

	public static String getDisplayDDate()
	{
		return MessageFormat.format("{0,time,dd}",
		new Object[]
		{ getCurrentTime() });
	}

	public static Date getCurrentDate()
	{
		SimpleDateFormat bartDateFormat = new SimpleDateFormat(
		"yyyy-MM-dd HH:mm:ss");

		Date date = new Date();

		String a = bartDateFormat.format(date);
		try
		{
		    date = bartDateFormat.parse(a);
		} catch (ParseException e)
		{
			e.printStackTrace();
		}
		return date;
	}
	
	public static long days(Date big_date,Date small_date){
	    return days(WgrDate.date2Str(big_date, "yyyy-MM-dd"), WgrDate.date2Str(small_date, "yyyy-MM-dd"));
	}

	public static long days(String big_date, String small_date)
	{
		long days = 0L;
		try
		{
			SimpleDateFormat formatter = new SimpleDateFormat(
			"yyyy-MM-dd");
			ParsePosition pos = new ParsePosition(0);
			ParsePosition pos1 = new ParsePosition(0);

			Date dt1 = formatter.parse(big_date, pos);
			Date dt2 = formatter.parse(small_date, pos1);

			days = (dt1.getTime() - dt2.getTime()) / 3600000L / 24L;
		} catch (Exception e)
		{
			System.out.println("exception " + e.toString());
		}
		return days;
	}
	
	/**
	 * 得到上一天
	 * @param ndate
	 * @param day
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static String beforeday(String ndate, int day)
	{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date myDate = null;
		try
		{
			myDate = sdf.parse(ndate);

			myDate.setDate(myDate.getDate() - day);
		} catch (ParseException e)
		{
			e.printStackTrace();
		}
		return sdf.format(myDate);
	}

	/**
	 * 得到下一天
	 * @param ndate
	 * @param day
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static String afterday(String ndate, int day)
	{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date myDate = null;
		try
		{
			myDate = sdf.parse(ndate);

			myDate.setDate(myDate.getDate() + day);
		} catch (ParseException e)
		{
			e.printStackTrace();
		}
		return sdf.format(myDate);
	}

	public static int getDayNumInMonth(String date)
	{
		int[] month =
		{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		StringTokenizer st = new StringTokenizer(date, "-");
		if (st.countTokens() != 3)
		{
			return -1;
		}
		int year = Integer.parseInt(st.nextToken());
		int tmpMonth = Integer.parseInt(st.nextToken());
		int dayNum = month[(tmpMonth - 1)];
		if ((tmpMonth == 2) && (year % 4 == 0) && (((year % 100 != 0) || (year % 400 == 0))))
		{
			++dayNum;
		}
		return dayNum;
	}

	public static String date2Str(Date dt, String dateformat)
	{
		String rt = null;

		if (dt != null)
		{
			try
			{
				SimpleDateFormat sdf = new SimpleDateFormat(dateformat);
				rt = sdf.format(dt);
			} catch (IllegalArgumentException localIllegalArgumentException)
			{
			}
		}
		return rt;
	}

	public static Date str2Date(String date, String dateformat)
	{
		SimpleDateFormat sdf = new SimpleDateFormat(dateformat);
		try
		{
			Date rtdate = sdf.parse(date);
			return rtdate;
		} catch (ParseException e)
		{
		    e.printStackTrace();
		}
		return null;
	}

	public boolean validate(String datestr)
	{
		if (datestr != null)
		{
			Pattern p = Pattern
					.compile("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\.\\s]?((((0?[13578])|(1[02]))[\\-\\/\\.\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\.\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\.\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\.\\s]?((((0?[13578])|(1[02]))[\\-\\/\\.\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\.\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\.\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1-2][0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$");
			return p.matcher(datestr).matches();
		}
		return false;
	}

	public static String appendYear(String val)
	{
		String month = "";
		if (val.matches("([0-9]{1,2}+)\\.([0-9]{1,2}+)"))
		{
			month = val.substring(0, val.lastIndexOf("."));

			if (compMonth(Integer.valueOf(month).intValue()))
			{
				val = getDisplayYDate() + "." + val;
			}
			else
			{
				val = String.valueOf(Integer.valueOf(getDisplayYDate())
						.intValue() - 1) + "." + val;
			}
		}
		if (val.matches("([0-9]{1,2}+)-([0-9]{1,2}+)"))
		{
			month = val.substring(0, val.lastIndexOf("-"));
			if (compMonth(Integer.valueOf(month).intValue()))
			{
				val = getDisplayYDate() + "-" + val;
			}
			else
			{
				val = String.valueOf(Integer.valueOf(getDisplayYDate())
						.intValue() - 1) + "-" + val;
			}
		}
		return val;
	}

	public static boolean compMonth(int value)
	{
		return (Integer.valueOf(getDisplayMDate()).intValue() < value);
	}

	public static synchronized boolean isLeapYear(int year)
	{
		if (year % 400 == 0) return true;
		if (year % 4 == 0)
		{
			return (year % 100 == 0);
		}

		return false;
	}

	public static String getChineseDate()
	{
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd",
				new DateFormatSymbols());
		String dtrDate = df.format(new Date());
		return dtrDate.substring(0, 4) + "年" + Integer.parseInt(dtrDate
				.substring(4, 6)) + "月" + Integer.parseInt(dtrDate.substring(6,
				8)) + "日";
	}

	public static int calBetweenTwoMonth(String dealMonth, String alterMonth)
	{
		int length = 0;
		if ((dealMonth.length() != 6) || (alterMonth.length() != 6))
		{
			length = -1;
		}
		else
		{
			int dealInt = Integer.parseInt(dealMonth);
			int alterInt = Integer.parseInt(alterMonth);
			if (dealInt < alterInt)
			{
				length = -2;
			}
			else
			{
				int dealYearInt = Integer.parseInt(dealMonth.substring(0, 4));
				int dealMonthInt = Integer.parseInt(dealMonth.substring(4, 6));
				int alterYearInt = Integer.parseInt(alterMonth.substring(0, 4));
				int alterMonthInt = Integer
						.parseInt(alterMonth.substring(4, 6));
				length = (dealYearInt - alterYearInt) * 12 + dealMonthInt - alterMonthInt;
			}
		}
		return length;
	}

	public static String increaseYearMonth(String yearMonth)
	{
		int year = new Integer(yearMonth.substring(0, 4)).intValue();
		/* 482 */int month = new Integer(yearMonth.substring(4, 6))
				.intValue();
		/* 483 */if ((++month <= 12) && (month >= 10))
			/* 484 */return yearMonth.substring(0, 4) + new Integer(month)
					.toString();
		/* 485 */if (month < 10)
		{
			/* 486 */return yearMonth.substring(0, 4) + "0" +
			/* 487 */new Integer(month).toString();
		}
		/* 489 */return new Integer(year + 1).toString() + "0" +
		/* 490 */new Integer(month - 12).toString();
	}

	public static String increaseYearMonth(String yearMonth, int addMonth)
	{
		/* 506 */int year = new Integer(yearMonth.substring(0, 4)).intValue();
		/* 507 */int month = new Integer(yearMonth.substring(4, 6))
				.intValue();
		/* 508 */month += addMonth;
		/* 509 */year += month / 12;
		/* 510 */month %= 12;
		/* 511 */if ((month <= 12) && (month >= 10))
		{
			/* 512 */return year + new Integer(month).toString();
		}
		/* 514 */return year + "0" + new Integer(month).toString();
	}

	public static String descreaseYearMonth(String yearMonth)
	{
		/* 527 */int year = new Integer(yearMonth.substring(0, 4)).intValue();
		/* 528 */int month = new Integer(yearMonth.substring(4, 6))
				.intValue();
		/* 529 */if (--month >= 10)
			/* 530 */return yearMonth.substring(0, 4) + new Integer(month)
					.toString();
		/* 531 */if ((month > 0) && (month < 10))
		{
			/* 532 */return yearMonth.substring(0, 4) + "0" +
			/* 533 */new Integer(month).toString();
		}
		/* 535 */return new Integer(year - 1).toString() +
		/* 536 */new Integer(month + 12).toString();
	}

	public static boolean yearMonthGreatEqual(String s1, String s2)
	{
		String temp1 = s1.substring(0, 4);
		String temp2 = s2.substring(0, 4);
		String temp3 = s1.substring(4, 6);
		String temp4 = s2.substring(4, 6);
		if (Integer.parseInt(temp1) > Integer.parseInt(temp2))
			return true;
		/* 575 */if (Integer.parseInt(temp1) == Integer.parseInt(temp2))
		{
			/* 576 */return (Integer.parseInt(temp3) < Integer
					.parseInt(temp4));
		}
		/* 578 */return false;
	}

	public static synchronized boolean isLeapYear()
	{
		Calendar cal = Calendar.getInstance();
		int year = cal.get(1);
		return isLeapYear(year);
	}

	public static synchronized Date getPreviousWeekDay(Date date)
	{
		GregorianCalendar gc = (GregorianCalendar) Calendar
		.getInstance();
		gc.setTime(date);
		return getPreviousWeekDay(gc);
	}

	public static synchronized Date getPreviousWeekDay(Calendar gc)
	{
		/* 615 */switch (gc.get(7))
		{
		case 2:
			/* 618 */
			gc.add(5, -3);
			/* 619 */break;
		case 1:
			/* 621 */
			gc.add(5, -2);
			/* 622 */break;
		default:
			/* 624 */
			gc.add(5, -1);
		}

		/* 627 */return gc.getTime();
	}

	public static synchronized Date getNextWeekDay(Date date)
	{
		/* 636 */GregorianCalendar gc = (GregorianCalendar) Calendar
				.getInstance();
		/* 637 */gc.setTime(date);
		/* 638 */switch (gc.get(7))
		{
		case 6:
			/* 641 */
			gc.add(5, 3);
			/* 642 */break;
		case 7:
			/* 644 */
			gc.add(5, 2);
			/* 645 */break;
		default:
			/* 647 */
			gc.add(5, 1);
		}

		/* 650 */return gc.getTime();
	}

	public static synchronized Calendar getNextWeekDay(Calendar gc)
	{
		/* 659 */switch (gc.get(7))
		{
		case 6:
			/* 662 */
			gc.add(5, 3);
			/* 663 */break;
		case 7:
			/* 665 */
			gc.add(5, 2);
			/* 666 */break;
		default:
			/* 668 */
			gc.add(5, 1);
		}

		/* 671 */return gc;
	}

	public static synchronized Date getLastDayOfNextMonth(Date date)
	{
		GregorianCalendar gc = (GregorianCalendar) Calendar
				.getInstance();
		gc.setTime(date);
		gc.setTime(getNextMonth(gc.getTime()));
		gc.setTime(getLastDayOfMonth(gc.getTime()));
		return gc.getTime();
	}

	public static synchronized Date getLastDayOfNextWeek(Date date)
	{
		GregorianCalendar gc = (GregorianCalendar) Calendar
		.getInstance();
		gc.setTime(date);
		gc.setTime(getNextWeek(gc.getTime()));
		gc.setTime(getLastDayOfWeek(gc.getTime()));
		return gc.getTime();
	}

	public static synchronized Date getFirstDayOfNextMonth(Date date)
	{
		GregorianCalendar gc = (GregorianCalendar) Calendar
		.getInstance();
		gc.setTime(date);
		gc.setTime(getNextMonth(gc.getTime()));
		gc.setTime(getFirstDayOfMonth(gc.getTime()));
		return gc.getTime();
	}

	public static synchronized Calendar getFirstDayOfNextMonth(Calendar gc)
	{
		gc.setTime(getNextMonth(gc.getTime()));
		gc.setTime(getFirstDayOfMonth(gc.getTime()));
		return gc;
	}

	public static synchronized Date getFirstDayOfNextWeek(Date date)
	{
		GregorianCalendar gc = (GregorianCalendar) Calendar
		.getInstance();
		gc.setTime(date);
		gc.setTime(getNextWeek(gc.getTime()));
		gc.setTime(getFirstDayOfWeek(gc.getTime()));
		return gc.getTime();
	}

	public static synchronized Calendar getFirstDayOfNextWeek(Calendar gc)
	{
		gc.setTime(getNextWeek(gc.getTime()));
		gc.setTime(getFirstDayOfWeek(gc.getTime()));
		return gc;
	}

	public static synchronized Date getNextMonth(Date date)
	{
		GregorianCalendar gc = (GregorianCalendar) Calendar
				.getInstance();
		gc.setTime(date);
		gc.add(2, 1);
		return gc.getTime();
	}

	public static synchronized Calendar getNextMonth(Calendar gc)
	{
		gc.add(2, 1);
		return gc;
	}

	public static synchronized Date getNextDay(Date date)
	{
		GregorianCalendar gc = (GregorianCalendar) Calendar
				.getInstance();
		gc.setTime(date);
		gc.add(5, 1);
		return gc.getTime();
	}

	public static synchronized Calendar getNextDay(Calendar gc)
	{
		gc.add(5, 1);
		return gc;
	}

	public static synchronized Date getNextWeek(Date date)
	{
		/* 844 */GregorianCalendar gc = (GregorianCalendar) Calendar
				.getInstance();
		/* 845 */gc.setTime(date);
		/* 846 */gc.add(5, 7);
		/* 847 */return gc.getTime();
	}

	public static synchronized Calendar getNextWeek(Calendar gc)
	{
		gc.add(5, 7);
		return gc;
	}

	public static synchronized Date getLastDayOfWeek(Date date)
	{
		/* 875 */GregorianCalendar gc = (GregorianCalendar) Calendar
		.getInstance();
		gc.setTime(date);
		switch (gc.get(7))
		{
		case 1:
			/* 880 */
			gc.add(5, 6);
			/* 881 */break;
		case 2:
			/* 883 */
			gc.add(5, 5);
			/* 884 */break;
		case 3:
			/* 886 */
			gc.add(5, 4);
			/* 887 */break;
		case 4:
			/* 889 */
			gc.add(5, 3);
			/* 890 */break;
		case 5:
			/* 892 */
			gc.add(5, 2);
			/* 893 */break;
		case 6:
			/* 895 */
			gc.add(5, 1);
			/* 896 */break;
		case 7:
			/* 898 */
			gc.add(5, 0);
		}

		/* 901 */return gc.getTime();
	}

	public static synchronized Date getFirstDayOfWeek(Date date)
	{
		/* 919 */GregorianCalendar gc = (GregorianCalendar) Calendar
				.getInstance();
		/* 920 */gc.setTime(date);
		/* 921 */switch (gc.get(7))
		{
		case 1:
			/* 924 */
			gc.add(5, 0);
			/* 925 */break;
		case 2:
			/* 927 */
			gc.add(5, -1);
			/* 928 */break;
		case 3:
			/* 930 */
			gc.add(5, -2);
			/* 931 */break;
		case 4:
			/* 933 */
			gc.add(5, -3);
			/* 934 */break;
		case 5:
			/* 936 */
			gc.add(5, -4);
			/* 937 */break;
		case 6:
			/* 939 */
			gc.add(5, -5);
			/* 940 */break;
		case 7:
			/* 942 */
			gc.add(5, -6);
		}

		/* 945 */return gc.getTime();
	}

	public static synchronized Calendar getFirstDayOfWeek(Calendar gc)
	{
		/* 956 */switch (gc.get(7))
		{
		case 1:
			/* 959 */
			gc.add(5, 0);
			/* 960 */break;
		case 2:
			/* 962 */
			gc.add(5, -1);
			/* 963 */break;
		case 3:
			/* 965 */
			gc.add(5, -2);
			/* 966 */break;
		case 4:
			/* 968 */
			gc.add(5, -3);
			/* 969 */break;
		case 5:
			/* 971 */
			gc.add(5, -4);
			/* 972 */break;
		case 6:
			/* 974 */
			gc.add(5, -5);
			/* 975 */break;
		case 7:
			/* 977 */
			gc.add(5, -6);
		}

		/* 980 */return gc;
	}

	public static synchronized Date getLastDayOfMonth(Date date)
	{
		GregorianCalendar gc = (GregorianCalendar) Calendar
		.getInstance();
		gc.setTime(date);
		switch (gc.get(2))
		{
		case 0:
			/* 1005 */
			gc.set(5, 31);
			/* 1006 */break;
		case 1:
			/* 1008 */
			gc.set(5, 28);
			/* 1009 */break;
		case 2:
			/* 1011 */
			gc.set(5, 31);
			/* 1012 */break;
		case 3:
			/* 1014 */
			gc.set(5, 30);
			/* 1015 */break;
		case 4:
			/* 1017 */
			gc.set(5, 31);
			/* 1018 */break;
		case 5:
			/* 1020 */
			gc.set(5, 30);
			/* 1021 */break;
		case 6:
			/* 1023 */
			gc.set(5, 31);
			/* 1024 */break;
		case 7:
			/* 1026 */
			gc.set(5, 31);
			/* 1027 */break;
		case 8:
			/* 1029 */
			gc.set(5, 30);
			/* 1030 */break;
		case 9:
			/* 1032 */
			gc.set(5, 31);
			/* 1033 */break;
		case 10:
			/* 1035 */
			gc.set(5, 30);
			/* 1036 */break;
		case 11:
			/* 1038 */
			gc.set(5, 31);
		}

		if ((gc.get(2) == 1) && (isLeapYear(
		gc.get(1))))
		{
			/* 1045 */gc.set(5, 29);
		}
		/* 1047 */return gc.getTime();
	}

	public static synchronized Calendar getLastDayOfMonth(Calendar gc)
	{
		switch (gc.get(2))
		{
		case 0:
			gc.set(5, 31);
			break;
		case 1:
			gc.set(5, 28);
			break;
		case 2:
			gc.set(5, 31);
			break;
		case 3:
			gc.set(5, 30);
			break;
		case 4:
			gc.set(5, 31);
			break;
		case 5:
			gc.set(5, 30);
			break;
		case 6:
			gc.set(5, 31);
			break;
		case 7:
			gc.set(5, 31);
			break;
		case 8:
			gc.set(5, 30);
			break;
		case 9:
			gc.set(5, 31);
			break;
		case 10:
			gc.set(5, 30);
			break;
		case 11:
			gc.set(5, 31);
		}

		if ((gc.get(2) == 1) && (isLeapYear(
		gc.get(1))))
		{
			gc.set(5, 29);
		}
		return gc;
	}

	public static synchronized Date getFirstDayOfMonth(Date date)
	{
		GregorianCalendar gc = (GregorianCalendar) Calendar
				.getInstance();
		gc.setTime(date);
		gc.set(5, 1);
		return gc.getTime();
	}

	public static synchronized Calendar getFirstDayOfMonth(Calendar gc)
	{
		gc.set(5, 1);
		return gc;
	}
	
	/**
	 * @return
	 */
	public static String days_second(String date_da, String date_xiao)
	{
		long seconds = 0L;
		String result = "00:00:00";
		try
		{
			SimpleDateFormat formatter = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			ParsePosition pos = new ParsePosition(0);
			ParsePosition pos1 = new ParsePosition(0);

			Date dt1 = formatter.parse(date_da, pos);
			Date dt2 = formatter.parse(date_xiao, pos1);

			seconds = (dt1.getTime() - dt2.getTime()) / 1000L;

			long date = seconds / 86400L;
			long hour = (seconds - (date * 24L * 60L * 60L)) / 3600L;
			long minut = (seconds - (date * 24L * 60L * 60L) - (hour * 60L * 60L)) / 60L;
			long second = seconds - (date * 24L * 60L * 60L) - (hour * 60L * 60L) - (minut * 60L);

			String hour_ = "";
			String minut_ = "";
			String second_ = "";

			if (hour == 0L)
				hour_ = "00";
			else if (hour + "".length() == 1L)
				hour_ = "0" + hour;
			else
				hour_ = hour + "";

			if (minut == 0L)
				minut_ = "00";
			else if (minut + "".length() == 1L)
				minut_ = "0" + minut;
			else
				minut_ = minut + "";

			if (second == 0L)
				second_ = "00";
			else if (second + "".length() == 1L)
				second_ = "0" + second;
			else
				second_ = second + "";

			if ((hour < 0L) || (minut < 0L) || (second < 0L))
			{
				result = "00:00:00";
			}

			result = hour_ + ":" + minut_ + ":" + second_;
		} catch (Exception e)
		{
			System.out.println("exception " + e.toString());
		}
		return result;
	}
	
	/**
     * @param date_da
     * @param date_xiao
     * @return
     */
    public static long days2second(String date_da, String date_xiao)
    {
        long seconds = 0L;
        try
        {
            SimpleDateFormat formatter = new SimpleDateFormat(
                    "yyyy-MM-dd HH:mm:ss");
            ParsePosition pos = new ParsePosition(0);
            ParsePosition pos1 = new ParsePosition(0);

            Date dt1 = formatter.parse(date_da, pos);
            Date dt2 = formatter.parse(date_xiao, pos1);

            seconds = (dt1.getTime() - dt2.getTime()) / 1000L;
        } catch (Exception e)
        {
            System.out.println("exception " + e.toString());
        }
        return seconds;
    }

	@SuppressWarnings("deprecation")
	public static String days_second(String ndate, int minute)
	{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date myDate = null;
		try
		{
			myDate = sdf.parse(ndate);

			myDate.setDate(myDate.getDate() + day);
		} catch (ParseException e)
		{
			e.printStackTrace();
		}

		Date newDate = new Date(myDate.getTime() + minute * 60 * 1000);
		return date2Str(newDate, "yyyy-MM-dd HH:mm:ss");
	}
	
	/**
	 * @param minute
	 * @return
	 */
	public static String days_second(int minute)
	{
		String result = "00:00:00";

		int hour = minute / 60;
		int minut = minute % 60;

		String hour_ = "";
		String minut_ = "";

		if (hour == 0)
			hour_ = "00";
		else if (hour + "".length() == 1)
			hour_ = "0" + hour;
		else
			hour_ = hour + "";

		if (minut == 0)
			minut_ = "00";
		else if (minut + "".length() == 1)
			minut_ = "0" + minut;
		else
			minut_ = minut + "";

		if ((hour < 0) || (minut < 0) || (second < 0))
		{
			result = "00:00:00";
		}

		result = hour_ + ":" + minut_ + ":00";
		return result;
	}

	public static int second2minute(String second)
	{
		String[] seconds = second.split(":");

		int result = 0;

		if (seconds[0].startsWith("0"))
		{
			result = Integer.parseInt(seconds[0].substring(1)) * 60;
		}
		else
		{
			result = Integer.parseInt(seconds[0]) * 60;
		}

		if (seconds[1].startsWith("0"))
		{
			result = result + Integer.parseInt(seconds[1].substring(1));
		}
		else
		{
			result = Integer.parseInt(seconds[1]);
		}

		return result;
	}
	
	public static Date parseDate(String date) {
        if (date == null) return null;
        DateFormat formatter = new DateFormat();
        formatter.setLenient(false);
        return formatter.parse(date);
    }
	
	public static Date parseDate(String date, String patter) {
        if (date == null) return null;
        formatter.setTimeZone(TimeZone.getDefault());
        formatter.applyPattern(patter);

        try {
            return formatter.parse(date);
        } catch (ParseException e) {
            return null;
        }
    }
	
    public static int getWeekDay(Date date) {
        Calendar cale = Calendar.getInstance();
        cale.setTime(date);
        return cale.get(Calendar.DAY_OF_WEEK) - 1;
    }
	
	public static int getWeekDay(String strDay) {

        Date day = parseDate(strDay);
        Calendar cale = Calendar.getInstance();
        cale.setTime(day);
        return cale.get(Calendar.DAY_OF_WEEK);
    }
	
	public static List getXingQIWu(String startdate, String enddate)
	{
	    List list =  new ArrayList();
	    
	    long jian = days(enddate,startdate);
	    
	    int cur = getWeekDay(startdate);
	    if(cur==6)
        {
            list.add(startdate);
        }
	    
	    for(int i=0;i<jian;i++)
	    {
	        String cur_date = afterday(startdate,i+1);
	        int cur_xingqi = getWeekDay(cur_date);
	        if(cur_xingqi==6)
	        {
	            list.add(cur_date);
	        }
	    }
	    return list;
	}
	
	@SuppressWarnings("unchecked")
    public static List getMonthDay(String startdate, String enddate)
	{
	    List list =  new ArrayList();
	    
	    int s_year  = Integer.parseInt(startdate.substring(0,4));
	    int e_year  = Integer.parseInt(enddate.substring(0,4));
	    String s_month = startdate.substring(5,7);
	    String e_month = enddate.substring(5,7);
	    int s_m = 1;
	    int e_m = 1;
	    if(s_month.startsWith("0"))
	    {
	        s_m = Integer.parseInt(s_month.replaceAll("0", ""));
	    }
	    else
	    {
	        s_m = Integer.parseInt(s_month);
	    }
	    if(e_month.startsWith("0"))
        {
	        e_m = Integer.parseInt(e_month.replaceAll("0", ""));
        }
        else
        {
            e_m = Integer.parseInt(e_month);
        }
	    
	    for(int i = s_year; i<=e_year;i++)
	    {
	        for(int j = 1; j<=12; j++)
	        {
	            String m = j+"";
	            if(j>9){
	                m = j+"";
	            }else{
	                m = "0"+j;
	            }
	            
	            if(i==s_year&&j>=s_m)
	            {
                    list.add(i+"-"+m+"-01");
	            }
	            else if(i==e_year&&j<=e_m)
	            {
                    list.add(i+"-"+m+"-01");
	            }
	            else if(i!=e_year)
	            {
	                list.add(i+"-"+m+"-01");
	            }
	        }
	    }
	    
	    return list;
	}
	
	public static Date workDays(Date startDate, int days) {
        Date endDate = startDate;
        Calendar cal = Calendar.getInstance();
        cal.setTime(startDate);
        int count = 1;
        while (count < days) {
            int day = cal.get(Calendar.DAY_OF_WEEK);
            if (day < 7 && day > 1)
                count++;
            cal.add(Calendar.DATE, 1);
            endDate = cal.getTime();
        }
        return endDate;
    }
	
	public static String str2ChinaSmall(String dateStr) {
	    
	    // 传参数的格式为 2011-11-26
	    String resultStr = "";
	    String[] ss = dateStr.split("-");

	    for (int j = 0; j < ss[0].length(); j++) {
	     switch (ss[0].charAt(j)) {
	     case '0':
	      resultStr += "〇";
	      break;
	     case '1':
	      resultStr += "一";
	      break;
	     case '2':
	      resultStr += "二";
	      break;
	     case '3':
	      resultStr += "三";
	      break;
	     case '4':
	      resultStr += "四";
	      break;
	     case '5':
	      resultStr += "五";
	      break;
	     case '6':
	      resultStr += "六";
	      break;
	     case '7':
	      resultStr += "七";
	      break;
	     case '8':
	      resultStr += "八";
	      break;
	     case '9':
	      resultStr += "九";
	      break;
	     }
	    }
	    resultStr = resultStr + "年";

	    if (ss[1].equals("01")) {
	     resultStr += "一";
	    } else if (ss[1].equals("02")) {
	     resultStr += "二";
	    } else if (ss[1].equals("03")) {
	     resultStr += "三";
	    } else if (ss[1].equals("04")) {
	     resultStr += "四";
	    } else if (ss[1].equals("05")) {
	     resultStr += "五";
	    } else if (ss[1].equals("06")) {
	     resultStr += "六";
	    } else if (ss[1].equals("07")) {
	     resultStr += "七";
	    } else if (ss[1].equals("08")) {
	     resultStr += "八";
	    } else if (ss[1].equals("09")) {
	     resultStr += "九";
	    } else if (ss[1].equals("10")) {
	     resultStr += "十";
	    } else if (ss[1].equals("11")) {
	     resultStr += "十一";
	    } else {
	     resultStr += "十二";
	    }
	    resultStr += "月";

	    if (ss[2].equals("01")) {
	     resultStr += "一";
	    } else if (ss[2].equals("02")) {
	     resultStr += "二";
	    } else if (ss[2].equals("03")) {
	     resultStr += "三";
	    } else if (ss[2].equals("04")) {
	     resultStr += "四";
	    } else if (ss[2].equals("05")) {
	     resultStr += "五";
	    } else if (ss[2].equals("06")) {
	     resultStr += "六";
	    } else if (ss[2].equals("07")) {
	     resultStr += "七";
	    } else if (ss[2].equals("08")) {
	     resultStr += "八";
	    } else if (ss[2].equals("09")) {
	     resultStr += "九";
	    } else if (ss[2].equals("10")) {
	     resultStr += "十";
	    } else if (ss[2].equals("11")) {
	     resultStr += "十一";
	    } else if (ss[2].equals("12")) {
	     resultStr += "十二";
	    } else if (ss[2].equals("13")) {
	     resultStr += "十三";
	    } else if (ss[2].equals("14")) {
	     resultStr += "十四";
	    } else if (ss[2].equals("15")) {
	     resultStr += "十五";
	    } else if (ss[2].equals("16")) {
	     resultStr += "十六";
	    } else if (ss[2].equals("17")) {
	     resultStr += "十七";
	    } else if (ss[2].equals("18")) {
	     resultStr += "十八";
	    } else if (ss[2].equals("19")) {
	     resultStr += "十九";
	    } else if (ss[2].equals("20")) {
	     resultStr += "二十";
	    } else if (ss[2].equals("21")) {
	     resultStr += "二十一";
	    } else if (ss[2].equals("22")) {
	     resultStr += "二十二";
	    } else if (ss[2].equals("23")) {
	     resultStr += "二十三";
	    } else if (ss[2].equals("24")) {
	     resultStr += "二十四";
	    } else if (ss[2].equals("25")) {
	     resultStr += "二十五";
	    } else if (ss[2].equals("26")) {
	     resultStr += "二十六";
	    } else if (ss[2].equals("27")) {
	     resultStr += "二十七";
	    } else if (ss[2].equals("28")) {
	     resultStr += "二十八";
	    } else if (ss[2].equals("29")) {
	     resultStr += "二十九";
	    } else if (ss[2].equals("30")) {
	     resultStr += "三十";
	    } else if (ss[2].equals("31")) {
	     resultStr += "三十一";
	    }
	    resultStr += "日";
	    return resultStr;
   }

	
	@SuppressWarnings("unused")
	public static void main(String[] args)
	{
//		SimpleDateFormat formatter = new SimpleDateFormat(
//				"yyyy-MM-dd HH:mm:ss");
//		ParsePosition pos = new ParsePosition(0);
//		ParsePosition pos1 = new ParsePosition(0);
//
//		Date dt1 = formatter.parse("2015-12-05 15:00:00", pos);
//		Date dt2 = formatter.parse("2015-12-03 13:00:00", pos1);
//
//		long diff = (dt1.getTime() - dt2.getTime()) / 1000L;
//		
//		
//		long nd = 1000*24*60*60;//一天的毫秒数
//		long nh = 1000*60*60;//一小时的毫秒数
//		long nm = 1000*60;//一分钟的毫秒数
//		long ns = 1000;//一秒钟的毫秒数long diff;
//		
//		long day = diff/nd;//计算差多少天
//		long hour = diff%nd/nh;//计算差多少小时
//		long min = diff%nd%nh/nm;//计算差多少分钟
//		long sec = diff%nd%nh%nm/ns;//计算差多少秒//输出结果
//		System.out.println("时间相差："+day+"天"+hour+"小时"+min+"分钟"+sec+"秒。");
		
//		System.out.println(WgrDate.addDay(new Date(), 1));
		
		//System.out.println(WgrDate.getWeekDay("2017-12-23"));

		//System.out.println(WgrDate.accountYear(21,8,WgrDate.getCurrentDate()));

        String date = "2019-01-21 17:00:00";
        String date2 = "2019-01-22";

        Date d1 = WgrDate.str2Date(date,"yyyy-MM-dd");
        Date d2 = WgrDate.str2Date(date2,"yyyy-MM-dd");
		//System.out.println(d1.compareTo(d2));

		WgrDate.accountRange("2019-02",21);
		//System.out.println("2019-01".substring(5));

		System.out.println(WgrDate.str2Date("2019-03-25 01:08:41","yyyy-MM-dd HH:mm:ss").getTime());
	}

    public static int getQuarter()
    {
        month = 1 + calendar.get(2);
        
        if(month==1||month==2||month==3){
            quarter = 1;
        }
        else if(month==4||month==5||month==6){
            quarter = 2;
        }
        else if(month==7||month==8||month==9){
            quarter = 3;
        }
        else{
            quarter = 4;
        }
        
        return quarter;
    }
    
    public static String getQuarterFirstDay(){
        int qu = getQuarter();
        String value = "";
        if(qu==1){
            value = WgrDate.getYear()+"-01-01";
        }else if(qu==2){
            value = WgrDate.getYear()+"-04-01";
        }else if(qu==3){
            value = WgrDate.getYear()+"-07-01";
        }else{
            value = WgrDate.getYear()+"-10-01";
        }
        return value;
    }
    
    public static String getQuarterLastDay(){
        int qu = getQuarter();
        String value2 = "";
        if(qu==1){
            value2= WgrDate.getYear()+"-03-31";
        }else if(qu==2){
            value2= WgrDate.getYear()+"-06-30";
        }else if(qu==3){
            value2= WgrDate.getYear()+"-09-30";
        }else{
            value2= WgrDate.getYear()+"-12-31";
        }
        return value2;
    }

    public static void setQuarter(int quarter)
    {
        WgrDate.quarter = quarter;
    }
    
    public static Date addOneSecond(Date date) {    
        Calendar calendar = Calendar.getInstance();    
        calendar.setTime(date);
        calendar.add(Calendar.SECOND, 1);    
        return calendar.getTime();    
    } 
    
    public static Date addSecond(Date date,int second) {    
        Calendar calendar = Calendar.getInstance();    
        calendar.setTime(date);
        calendar.add(Calendar.SECOND, second);    
        return calendar.getTime();    
    }
    
    public static Date addMinute(Date date,int minute){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, minute);
        return calendar.getTime();
    }
    
    public static Date addDay(Date date,int day){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, day);
        return calendar.getTime();
    }
    
    public static Date addHour(Date date,int minute){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR, minute);
        return calendar.getTime();
    }
    
    public static Date addMonth(Date date,int month){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, month);
        return calendar.getTime();
    }
    
    public static Date addYear(Date date,int year){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, year);
        return calendar.getTime();
    }

	public static String accountMonth(int gzday,int gztime,String date){
    	return accountMonth(gzday,gztime,WgrDate.str2Date(date,"yyyy-MM-dd HH:mm:ss"));
	}
	//结算年份
	public static String accountYear(int gzday,int gztime,Date date){
		//得到结算日期
		Date gz = getDate(gzday, gztime,date);

		String endTime;

		if (date.getTime() > gz.getTime()) {
			calendar.setTime(gz);
			calendar.add(Calendar.MONTH, +1);//加1个月
			Date Time = calendar.getTime();//统计结束时间
			endTime = new SimpleDateFormat("yyyy").format(Time);
		} else {
			endTime = new SimpleDateFormat("yyyy").format(gz);
		}
		return endTime;
	}

	//根据选择的日期和结算天数、小时得到结算月份
    public static String accountMonth(int gzday,int gztime,Date date){
		//得到结算日期
		Date gz = getDate(gzday, gztime,date);

		String endTime;
		if (date.getTime() > gz.getTime()) {
			calendar.setTime(gz);
			calendar.add(Calendar.MONTH, +1);//加1个月
			Date Time = calendar.getTime();//统计结束时间
			endTime = new SimpleDateFormat("yyyy-MM").format(Time);
		} else {
			endTime = new SimpleDateFormat("yyyy-MM").format(date);
		}
    	return endTime;
	}
	//结算天
	public static String accountDay(int gzday,int gztime,Date date){
		String endTime;
		calendar.setTime(date);
		int newTime = calendar.get(Calendar.HOUR_OF_DAY);

		if (newTime < gztime) {
			calendar.setTime(date);
			calendar.add(Calendar.DATE, -1);//减去一天
			Date Time = calendar.getTime();//统计结束时间
			endTime = new SimpleDateFormat("yyyy-MM-dd").format(Time);
		} else {
			endTime = new SimpleDateFormat("yyyy-MM-dd").format(date);
		}
		return endTime;
	}
	//根据选择的日期和结算天数、小时得到结算日期范围
	public static Map accountDate(int gzday,int gztime,Date date){
    	Map map = new HashMap();
    	//得到结算日期
		Date gz = getDate(gzday, gztime,date);

		if (date.getTime() > gz.getTime()) {
			calendar.setTime(gz);
			calendar.add(Calendar.MONTH, +1);//加1个月
			Date Time = calendar.getTime();//统计结束时间

			map.put("beginTime",new SimpleDateFormat("yyyy-MM-dd").format(gz));
			map.put("endTime",new SimpleDateFormat("yyyy-MM-dd").format(Time));
		} else {
			calendar.setTime(gz);
			calendar.add(Calendar.MONTH, -1);//减1个月
			Date Time = calendar.getTime();//开始时间
			map.put("beginTime",new SimpleDateFormat("yyyy-MM-dd").format(Time));
			map.put("endTime",new SimpleDateFormat("yyyy-MM-dd").format(gz));
		}
		return map;
	}
	private static Date getDate(int gzday, int gztime,Date date) {
		calendar.setTime(date);
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH)+1;
		String gzDate = year + "-" + month + "-" + gzday + " " + gztime + ":" + "00:00";

		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		ParsePosition pos = new ParsePosition(0);
		return formatter.parse(gzDate,pos);
	}

	//结算天
	public static String[] accountRange(String accountMonth,int day){
		String[] range = new String[2];
		Calendar tempStart = Calendar.getInstance();
		tempStart.setTime(WgrDate.str2Date(accountMonth+"-"+day,"yyyy-MM-dd"));
		tempStart.add(Calendar.MONTH, -1);
		range[0] = WgrDate.date2Str(tempStart.getTime(),"yyyy-MM-dd");
		range[1] = accountMonth+"-"+(day-1);
		return range;
	}

	public static List range(String startDate,String endDate){

		List dayList = new ArrayList();

		long tian = WgrDate.days(endDate,startDate);

		List<Date> result = new ArrayList<Date>();
		Calendar tempStart = Calendar.getInstance();
		tempStart.setTime(WgrDate.str2Date(startDate,"yyyy-MM-dd"));
		//tempStart.add(Calendar.DAY_OF_YEAR, 1);

		Calendar tempEnd = Calendar.getInstance();
		tempEnd.setTime(WgrDate.str2Date(endDate,"yyyy-MM-dd"));
		while (tempStart.compareTo(tempEnd)<=0) {
			Map map = new HashMap();
			result.add(tempStart.getTime());

			int year = tempStart.get(Calendar.YEAR);
			int month = tempStart.get(Calendar.MONTH)+1;
			int day = tempStart.get(Calendar.DAY_OF_MONTH);

			String yue ="";
			if(month>9){
				yue = ""+month;
			}else{
				yue = "0"+month;
			}

			String ri  ="";
			if(day>9){
				ri = ""+day;
			}else{
				ri = "0"+day;
			}

			//String yearMonth = startDate.substring(0,7);
			StringBuffer sb = new StringBuffer();

			if(!tempStart.equals(tempEnd)){
				sb.append("select '"+year+"-"+yue+"-"+ri+"' as cur_day union ");
			}else{
				sb.append("select '"+year+"-"+yue+"-"+ri+"' as cur_day ");
			}
			map.put("cur_day",year+"-"+yue+"-"+ri);
			dayList.add(map);
			//System.out.println(sb.toString());
			tempStart.add(Calendar.DAY_OF_YEAR, 1);
		}
		return dayList;
	}
}